/**
 * 存储后端抽象接口
 * 
 * 提供统一的存储后端接口，支持本地、NFS、NVMe over Fabric等存储
 */

#ifndef XDEVICE_STORAGE_H
#define XDEVICE_STORAGE_H

#include <xdevice/xdevice.h>

#ifdef __cplusplus
extern "C" {
#endif

/* ========== 内部类型 ========== */
typedef struct xdevice_storage_backend xdevice_storage_backend_t;

/* ========== 存储后端操作表 ========== */
typedef struct {
    int (*init)(xdevice_storage_backend_t* backend, const char* config);
    int (*cleanup)(xdevice_storage_backend_t* backend);
    int (*create_device)(xdevice_storage_backend_t* backend, const char* name, uint64_t size);
    int (*open_device)(xdevice_storage_backend_t* backend, const char* name);
    int (*close_device)(xdevice_storage_backend_t* backend, const char* name);
    int64_t (*read)(xdevice_storage_backend_t* backend, const char* name, uint64_t offset, void* buffer, size_t length);
    int64_t (*write)(xdevice_storage_backend_t* backend, const char* name, uint64_t offset, const void* buffer, size_t length);
    int (*sync)(xdevice_storage_backend_t* backend, const char* name);
    int (*get_device_info)(xdevice_storage_backend_t* backend, const char* name, xdevice_device_info_t* info);
    int (*list_devices)(xdevice_storage_backend_t* backend, char names[][XDEVICE_MAX_DEVICE_NAME], int max_count);
} xdevice_storage_ops_t;

/* ========== 存储后端结构 ========== */
struct xdevice_storage_backend {
    xdevice_storage_type_t type;
    const xdevice_storage_ops_t* ops;
    char config_path[XDEVICE_MAX_PATH_LEN];
    void* private_data;                 // 后端私有数据
    bool initialized;
};

/* ========== 存储管理器API ========== */

/**
 * 创建存储后端
 * @param type 存储类型
 * @param config 配置路径或参数
 * @return 后端实例，失败返回NULL
 */
xdevice_storage_backend_t* xdevice_storage_create(xdevice_storage_type_t type, const char* config);

/**
 * 销毁存储后端
 * @param backend 后端实例
 */
void xdevice_storage_destroy(xdevice_storage_backend_t* backend);

/**
 * 注册存储后端
 * @param type 存储类型
 * @param ops 操作表
 * @return 错误码
 */
int xdevice_storage_register(xdevice_storage_type_t type, const xdevice_storage_ops_t* ops);

/* ========== 存储后端实现注册函数 ========== */
int xdevice_storage_local_register(void);
int xdevice_storage_mock_register(void);

#ifdef __cplusplus
}
#endif

#endif // XDEVICE_STORAGE_H
